package sg.edu.nus.iss.billing.gui;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.List;

import sg.edu.nus.iss.billing.BillingApplication;
import sg.edu.nus.iss.billing.BillingConstants;
import sg.edu.nus.iss.billing.model.account.Account;
import sg.edu.nus.iss.billing.model.subscription.AdditionalChannelSubscription;
import sg.edu.nus.iss.billing.model.subscription.CableSubscription;
import sg.edu.nus.iss.billing.model.subscription.DigitalSubscription;
import sg.edu.nus.iss.billing.model.subscription.ISubscribable;
import sg.edu.nus.iss.billing.model.subscription.MobileSubscription;
import sg.edu.nus.iss.billing.model.subscription.Service;
import sg.edu.nus.iss.billing.model.subscription.Subscription;

/**
 * @author Yeo Hee Ling, Ananda, Saravana
 *
 */
public class SubscriptionHistoryPanel extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1L;
	
	private Properties properties;
	private BillingApplication billingAppl;
	private Account account;
	private JPanel searchResultPanel;
	private Dictionary<String, String> IdToNameMapping;

	private JButton btnSearchResult;
	
	public SubscriptionHistoryPanel(BillingApplication billingAppl, Account account, JPanel searchResultPanel) {
		this.account = account;
		this.billingAppl = billingAppl;
		this.searchResultPanel = searchResultPanel;
		this.setOpaque(false);
		loadConfiguration();
		
		GridBagLayout layout = new GridBagLayout();
		this.setLayout(layout);
		
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.insets = new Insets(5, 5, 5, 5);
		constraints.fill = GridBagConstraints.HORIZONTAL;
		
		constraints.gridx = 0;
		constraints.gridy = 0;
		this.add(getHeaderPanel(), constraints);
		
		constraints.gridx = 0;
		constraints.gridy = 1;
		JScrollPane scrollContentPanel = new JScrollPane(getContentPanel());
		scrollContentPanel.setPreferredSize(new Dimension(900,500));
		this.add(scrollContentPanel, constraints);
		
		constraints.gridx = 0;
		constraints.gridy = 2;
		this.add(getButtonPanel(), constraints);
	}
	
	private void loadConfiguration() {
		try {
			properties = new Properties();
			FileInputStream in = new FileInputStream(BillingConstants.PROPERTIES_SUBSCRIPTION);
			properties.load(in);
			in.close();
		}
		catch(Exception ex) {
			ex.printStackTrace();
		}
		
		IdToNameMapping = new Hashtable<String, String>();
		
		IdToNameMapping.put(DigitalSubscription.ID, properties.getProperty("label.subscription.digitalVoice"));
		IdToNameMapping.put(DigitalSubscription.Services.IDD.getID(), properties.getProperty("label.subscription.IDDCalls"));
		IdToNameMapping.put(DigitalSubscription.Services.CALLTRANSFER.getID(), properties.getProperty("label.subscription.callTransfer"));
		
		IdToNameMapping.put(MobileSubscription.ID, properties.getProperty("label.subscription.mobileVoice"));
		IdToNameMapping.put(MobileSubscription.Services.IDD.getID(), properties.getProperty("label.subscription.IDDCalls"));
		IdToNameMapping.put(MobileSubscription.Services.DATASERVICE.getID(), properties.getProperty("label.subscription.dataService"));
		IdToNameMapping.put(MobileSubscription.Services.ROAMING.getID(), properties.getProperty("label.subscription.roaming"));
		
		IdToNameMapping.put(CableSubscription.ID, properties.getProperty("label.subscription.cableTV"));
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnSearchResult) {
			billingAppl.displayPage(searchResultPanel);
		}
	}
	
	private JPanel getHeaderPanel() {
		JPanel headerPanel = new JPanel();
		headerPanel.setOpaque(false);
		headerPanel.setLayout(new GridLayout(0, 1, 10, 10));
		
		JLabel msgLabel = new JLabel(properties.getProperty("title.viewSubscriptionHistory"));
		msgLabel.setFont(new Font("Tahoma", Font.BOLD, 18));
		msgLabel.setHorizontalAlignment(SwingConstants.CENTER);
		headerPanel.add(msgLabel);
		
		ImageIcon imgDivider = new ImageIcon("./images/divider.jpg");
		JLabel lblDivider = new JLabel(imgDivider);
		headerPanel.add(lblDivider);
		
		JPanel custAccountInfoPanel = new JPanel();
		custAccountInfoPanel.setOpaque(false);
		
		JTextField accountNumTextField = new JTextField(15);
		accountNumTextField.setText(account.getAccountNo());
		accountNumTextField.setEditable(false);
		custAccountInfoPanel.add(new JLabel(properties.getProperty("label.subscription.accountNo")));
		custAccountInfoPanel.add(accountNumTextField);
		
		JTextField txtCustomerNric= new JTextField(15);
		txtCustomerNric.setText(account.getCustomerNRIC());
		txtCustomerNric.setEditable(false);
		custAccountInfoPanel.add(new JLabel(properties.getProperty("label.subscription.nric")));
		custAccountInfoPanel.add(txtCustomerNric);
		
		JTextField txtCustomerName= new JTextField(15);
		txtCustomerName.setText(account.getCustomerName());
		txtCustomerName.setEditable(false);
		custAccountInfoPanel.add(new JLabel(properties.getProperty("label.subscription.name")));
		custAccountInfoPanel.add(txtCustomerName);
		
		headerPanel.add(custAccountInfoPanel);
		
		return headerPanel;
	}
	
	private JPanel getContentPanel() {
		JPanel contentPanel = new JPanel(new GridBagLayout());
		contentPanel.setOpaque(false);
		List<JLabel> cells;
		
		List<Subscription> subscriptionList = this.account.getSubscriptions();
		if(subscriptionList.size() == 0){
			contentPanel.add(new JLabel(properties.getProperty("label.subscription.noHistory")));
			return contentPanel;
		}
		
		//header
		cells = new ArrayList<JLabel>();
		cells.add(new JLabel());
		cells.add(new JLabel(properties.getProperty("label.subscription.commencedDt")));
		cells.add(new JLabel(properties.getProperty("label.subscription.terminatedDt")));
		addRow(contentPanel, 0, cells, false);
		
		List<String> planTypes = Arrays.asList(DigitalSubscription.ID, MobileSubscription.ID, CableSubscription.ID);
		int i = 1;
		for(String planType : planTypes) {
			for(Subscription plan : subscriptionList){
				if(!planType.equalsIgnoreCase(plan.getSubscriptionId())){
					continue;
				}
				
				cells = new ArrayList<JLabel>();
				String planName = IdToNameMapping.get(plan.getSubscriptionId());
				if(plan instanceof DigitalSubscription){
					planName += " (" + ((DigitalSubscription)plan).getTelephoneNumber() + ")";
				} else if(plan instanceof MobileSubscription){
					planName += " (" + ((MobileSubscription)plan).getTelephoneNumber() + ")";
				}
				cells.add(new JLabel(planName));
				cells.add(new JLabel(plan.getDateCommenced()));
				cells.add(new JLabel(plan.getDateTerminated()));
				addRow(contentPanel, i, cells, false);
				i++;
				
				//for cable tv, display the channels, not the services
				if(plan instanceof CableSubscription) {
					List<AdditionalChannelSubscription> channelList = ((CableSubscription)plan).getAdditionalChannelNames();
					for(AdditionalChannelSubscription channel : channelList){
						cells = new ArrayList<JLabel>();
						cells.add(new JLabel(properties.getProperty("label.subscription.additionalChannel") + " : " + channel.getChannelName()));
						cells.add(new JLabel(channel.getDateCommenced()));
						cells.add(new JLabel(channel.getDateTerminated()));
						addRow(contentPanel, i, cells, true);
						i++;
					}
				}
				else {
					for (ISubscribable iSubscribable :  plan.getServices()) {
						Service service = (Service) iSubscribable;
						
						//don't display the local services as it is not optional
						if (service.getServiceId().equals(DigitalSubscription.Services.LOCAL.getID()) || 
							service.getServiceId().equals(MobileSubscription.Services.LOCAL.getID())){
							continue;
						}
						
						cells = new ArrayList<JLabel>();
						cells.add(new JLabel(IdToNameMapping.get(service.getServiceId())));
						cells.add(new JLabel(service.getDateCommenced()));
						cells.add(new JLabel(service.getDateTerminated()));
						addRow(contentPanel, i, cells, true);
						i++;
					}
				}
			}
		}
		return contentPanel;
	}
	
	private void addRow(JPanel panel, int rowNum, List<JLabel> cells, boolean isOptionalFeature) {
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.fill =  GridBagConstraints.BOTH;
		constraints.gridy = rowNum;
		
		Border border = BorderFactory.createMatteBorder(0, 0, 0, 1, Color.BLACK);
		Border margin;
		if(isOptionalFeature) {
			margin = new EmptyBorder(0,15,0,15);
		} else {
			margin = new EmptyBorder(10,15,0,15);
		}
		
		int i = 0;
		for(JLabel cell : cells){
			
			cell.setOpaque(false);
			cell.setBorder(new CompoundBorder(border, margin));
			cell.setVerticalAlignment(SwingConstants.TOP);
			
			if(i == 0 && isOptionalFeature) 
				constraints.insets = new Insets(0, 25, 0, 0);
			else 
				constraints.insets = new Insets(0, 0, 0, 0);
			
			constraints.gridx = i;
			panel.add(cell, constraints);
			i++;
		}

	}
	
	private JPanel getButtonPanel() {
		JPanel buttonPanel = new JPanel();
		buttonPanel.setOpaque(false);
		
		btnSearchResult = new JButton(properties.getProperty("label.subscription.button.searchResult"));
		btnSearchResult.setPreferredSize(new Dimension(175, 25));
		btnSearchResult.addActionListener(this);
		buttonPanel.add(btnSearchResult);
		
		return buttonPanel;
	}
}
